//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   CATALOG_DTL
// AUTHOR           :   Saint Peng 
// DATE CREATE      :   2007/07/17
// DESCRIPTION      :   a ACTION file for the function, CATALOG_DTL
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2007/07/17   Saint Peng      1.0                 source is generated.
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.erp.app.catalog.catalogDtl.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.catalog.catalogDtl.criteria.CatalogDtlSrchCriteria;
import com.erp.app.catalog.catalogDtl.vo.CatalogDtlVo;
import com.erp.app.common.ProjectConstants;
import com.erp.app.util.web.WebUtil;
import com.erp.framework.controller.action.SimplePageEditStyleActionTemplate;
import com.erp.framework.controller.form.MultiRow;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.user.User;

/**
* A <strong>Page Edit Action</strong> is an adapter between the contents of an incoming
* HTTP request and the corresponding business logic that should be executed to
* process this request.  The controller (ActionServlet) will select an
* appropriate Action for each request.
* The main purpose for this action class is to handle the one-page document events
* from the web browsers.
*
* @author          Saint Peng 
* @version 1.0     2007/07/17
* @since           FrameWork 1.0
*
*/
public class CatalogDtlAction extends SimplePageEditStyleActionTemplate {
	
//	public static final String DTL = "Dtl";
//	public static final String ACTION_DEL_DTL = ACTION_DELETE_ROW + DTL;

	////////////////////////////////////////////////////////////////////////////////
	// CONSTANTS DEFINITION
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// GLOBAL VARIABLES DEFINITION
	////////////////////////////////////////////////////////////////////////////////



	////////////////////////////////////////////////////////////////////////////////
	// LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// END OF LOGICAL AND BUSINESS METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// OVERRIDES PARENT'S OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////

	//the following method is only to be overwritten by sub-class if intended
	//to set the aut-search on-load to on/off
	protected boolean getLoadOnEnter() {

		//auto-search when load
		return true;
	}

	//the following method is only to be overwritten by sub-class if intended
	//to set the clearing search result sets on-load to on/off
	protected boolean getResetOnEnter() {

		//reset last searched results when load 
		return true;
	}
	
	
	/* protected void afterDeleteRow(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
	            HttpServletResponse response, ActionErrors errors) {
	    HttpSession session = request.getSession(false);
		CatalogDtlForm form = (CatalogDtlForm) formIn;
		String action = form.getPageEvent();
		String pageEventSrc = form.getPageEventSrc();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
		ArrayList rows = form.getMultiRows();
		CatalogDtlRow row = (CatalogDtlRow) rows.get(Integer.parseInt(pageEventSrc));
		

	    }*/

		//TODO: Add the following line to ProjectSecurityConstants.java
		//public static final String SP_SHARE_CATALOG_DTL = "SP_SHARE_CATALOG_DTL";
	protected void afterAddRow(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {

		HttpSession session = request.getSession(false);
		CatalogDtlForm form = (CatalogDtlForm) formIn;
		String action = form.getPageEvent();
		ActionForward actionForward = new ActionForward(mapping.getInput());
		User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

		try {
			ArrayList rows = form.getMultiRows();
			if (rows != null && rows.size() > 0) {
				CatalogDtlRow row = (CatalogDtlRow) rows.get(rows.size() - 1);
				row.setDtlId(CommonUtil.getNextSequence(form.getMultiRows(), "dtlId"));
				row.setCompanyCode(WebUtil.getAppropriateCompanyCode(session, ProjectConstants.SP_SHARE_CATALOG_DTL));
			}
		} catch (Exception e) {
			Logger.logExceptions(this, e);
		}
	}

	protected String getSimpleSrchMenuId() {

		//TODO: Add the following line to ProjectMenuConstants.java
		//public static final String MOD_CATALOG_DTL = "MOD_CATALOG_DTL";
		return ProjectConstants.MOD_CATALOG_DTL;
	}

	////////////////////////////////////////////////////////////////////////////////
	// END OF OPTIONAL METHODS
	////////////////////////////////////////////////////////////////////////////////


	////////////////////////////////////////////////////////////////////////////////
	// IMPLEMENTS PARENT'S ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////

	protected String getModuleName(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request,HttpServletResponse response) {

		//TODO: Add the following line to ProjectMenuConstants.java
		//public static final String MOD_CATALOG_DTL = "MOD_CATALOG_DTL";
		return ProjectConstants.MOD_CATALOG_DTL;
	}

	protected String getFunctionId() {

		//struts-config xxxxx.do
		return "catalogDtl.do";
	}

	protected void setFormType(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
		 HttpServletResponse response, ActionErrors errors) {

		CatalogDtlForm form = (CatalogDtlForm) formIn;
		form.setFormType(ProjectConstants.FORM_TYPE_PAGE_EDIT);
	}

	protected String getMasterDelegateName() {

		//the corresponding delegate session name
		return "catalogDtlDelegate";
	}

	protected Class getMasterFormClass() {

		//the corresponding form class
		return CatalogDtlForm.class;
	}

	protected Class getMasterVOClass() {

		//corresponding master vo class
		return CatalogDtlVo.class;
	}

	protected Class getDetailVOClass() {

		//corresponding detail vo class
		return CatalogDtlVo.class;
	}

	protected Class getDetailFormClass() {
		//corresponding detail row class
		return CatalogDtlRow.class;
	}

	protected String getDetailFormSessionName() {

		//corresponding detail form session name
		return "catalogDtlForm";
	}

	protected Class getSearchCriteriaClass() {

		//corresponding searching criteria class
		return CatalogDtlSrchCriteria.class;
	}

	protected void setOtherCriteria(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response,
		ActionErrors errors, Object criteria) {

		HttpSession session = request.getSession(false);
		User user = (User) session.getAttribute(ProjectConstants.SESSION_USER);
		CatalogDtlSrchCriteria crit = (CatalogDtlSrchCriteria) criteria;

		//TODO: Add the following line to ProjectSecurityConstants.java
		//public static final String SP_SHARE_CATALOG_DTL = "SP_SHARE_CATALOG_DTL";
		String companyCode = WebUtil.getAppropriateCompanyCode(session, ProjectConstants.SP_SHARE_CATALOG_DTL);

		crit.setSrchCompanyCode(companyCode);
		crit.setSearchResult(true);
	}
	
	 protected void afterDeleteRow(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
	            HttpServletResponse response, ActionErrors errors) {
		 CatalogDtlCommonValidator validator = new CatalogDtlCommonValidator();
		 validator.validateDelDtlRow(mapping, formIn, request, response, errors);
		 if(errors.size() > 0){
			 CatalogDtlForm form = (CatalogDtlForm) formIn;
			 int pageEventSrc = Integer.parseInt(form.getPageEventSrc());

	         MultiRow row = (MultiRow) form.getMultiRows(0).get(pageEventSrc);
	         row.setIsDelete(""); 
		 }
	    }

	////////////////////////////////////////////////////////////////////////////////
	// END OF ABSTRACT METHODS
	////////////////////////////////////////////////////////////////////////////////
}
